package algotithm.leetcode.math.test7;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/25-22:29
 * @URL https://leetcode-cn.com/problems/reverse-integer/
 * @Title 7. 整数反转
 */
public class Solution {
    public int reverse(int x) {
        long res = 0;
        int flag = -1;
        if(x < 0) {
            x = -x;
        } else {
            flag = 1;
        }
        while(x != 0){
            res = res * 10 + x % 10;
            x /= 10;
        }
        res *= flag;
        if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
            return 0;
        } else {
            return (int)res;
        }
    }
}